package com.woniu.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.user.commons.Result;
import com.woniu.user.controller.form.SearchForm;
import com.woniu.user.model.Book;
import com.woniu.user.model.Order;
import com.woniu.user.service.AccountService;
import com.woniu.user.service.OrderService;
import com.woniu.user.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单信息表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private AccountService accountService;

    @GetMapping("myOrder")
    public Result myOrder(Integer current, Integer size, String condition, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        log.info("查看我的订单{}",userId);
        Page<Order> query = orderService.query(userId, current, size, condition);
        return Result.success(200, "查询成功", query);
    }

    @PutMapping("finishOrder")
    public Result finishOrder(Integer orderId) {
        log.info("教练已经上完课了{}", orderId);
        Order byId = orderService.getById(orderId);
        byId.setState("已完成");
        orderService.save(byId);
        return Result.success(200, "完成", null);
    }

    @GetMapping("getMyOrder")
    public Result getMyOrder(Integer page, Integer size, HttpServletRequest req){
        log.info("获取我的订单{} {} {}", page, size, req);
        Page<Order> orderPage = new Page<>(page, size);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        IPage<Order> myOrders = orderService.getMyOrder(userId, orderPage);
        return Result.success(200, "获取成功", myOrders);
    }

    // 学员付款
    @PutMapping("pay")
    public Result pay(Integer orderId, String password, HttpServletRequest req){
        log.info("学员付款{}", orderId);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        orderService.pay(orderId, userId, password);
        return Result.success(200, "付款成功", null);
    }

    // 学员取消订单
    @PutMapping("cancel")
    public Result cancel(Integer orderId, HttpServletRequest req){
        log.info("学员取消订单{}", orderId);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        orderService.cancel(orderId, userId);
        return Result.success(200, "取消成功", null);
    }

    // 学员上完课确认付款
    @PutMapping("checkPay")
    public Result checkPay(Integer orderId, String password, HttpServletRequest req){
        log.info("学员上完课确认付款 {}", orderId);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        orderService.checkPay(orderId, userId, password);
        return Result.success(200, "付款成功", null);
    }

    // 学员申请退款
    @PutMapping("applyBack")
    public Result applyBack(Integer orderId, String reason, HttpServletRequest req){
        log.info("学员申请退款{} {}", orderId, reason);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        orderService.applyBack(orderId, reason, userId);
        return Result.success(200, "申请成功", null);
    }

    // 关键词搜索
    @GetMapping("search")
    public Result search(SearchForm form, HttpServletRequest req){
        log.info("搜索订单{}", form);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        IPage<Order> myOrders = null;
        if(form.getKey() == null || "".equals(form.getKey())){
            Integer page = form.getPage();
            Integer size = form.getSize();
            Page<Order> orderPage = new Page<>(page, size);
            myOrders = orderService.getMyOrder(userId, orderPage);
            return Result.success(200, "搜索成功", myOrders);
        }
        myOrders =  orderService.search(form, userId);
        return Result.success(200, "搜索成功", myOrders);
    }
}

